define(function(){
	var ListTable=function (config){
		var listTable=this;

		//listTable.layoutClassName='layout-table';
		//config.tbodyScroll && ();

		listTable.$dom=$('<div class="listTable border-solid"></div>');

		listTable.$thead=$('<div class="thead"></div>').appendTo(listTable.$dom);
		listTable.$theadTr=$('<div class="tr"></div>').appendTo(listTable.$thead);

		if(config.tbodyScroll!==undefined){
			listTable.$tbodyScrollBox=$('<div class="tbodyScrollBox"></div>').appendTo(listTable.$dom);
			listTable.$tbody=$('<div class="tbody"></div>').appendTo(listTable.$tbodyScrollBox);
			listTable.$dom.addClass('layout-tbodyscroll');
			if(typeof config.tbodyScroll ==='number'){
				listTable.$tbodyScrollBox.css('max-height',config.tbodyScroll);
			}
		}else{
			listTable.$tbody=$('<div class="tbody"></div>').appendTo(listTable.$dom);
		}



		listTable.trTemplete=config.tr;
		listTable.TrRoot=config.TrRoot || function(){};

		listTable.trIdIndex=0;
		listTable.tbodyTrList={};


		!config.tableClassName && (config.tableClassName='');
		listTable.$dom.addClass(config.tableClassName);

		if(config.tableStyle!==undefined){
			listTable.$dom.css(config.tableStyle);
		}

		function TdDom(tdData){
			var tdDom=this;
			tdDom.$dom=$('<div class="td"></div>');
		}
		function theadTrInit(){
			var trDom={
				$dom:listTable.$theadTr
			}
			var theadTrRoot=new listTable.TrRoot(trDom);
			$.each(listTable.trTemplete,function(i,tdTemplete){
				var theadTdDom=new TdDom();
				if(tdTemplete.theadTd!==undefined){
					if(tdTemplete.theadTd.init!==undefined){
						tdTemplete.theadTd.init(theadTdDom.$dom,theadTrRoot);
					}
				}
				theadTdDom.$dom.appendTo(listTable.$theadTr);
			});
		}
		theadTrInit();

		function TbodyTrDom(){
			var trDom=this;
			trDom.$dom=$('<div class="tr"></div>');
			trDom.trRoot=new listTable.TrRoot(trDom);
			trDom.tdList=[];
			function tbodyTrInit(){
				$.each(listTable.trTemplete, function(i,tdTemplete) {
					var tdDom=new TdDom();
					if(tdTemplete.tbodyTd!==undefined){
						if(tdTemplete.tbodyTd.init!==undefined){
							tdTemplete.tbodyTd.init(tdDom.$dom,trDom.trRoot,trDom);
						}
					}
					tdDom.$dom.appendTo(trDom.$dom);
					trDom.tdList.push(tdDom);
				});
			}
			tbodyTrInit();
			trDom.update=function(trData){
				if(trData!==undefined){
					trDom.data=trData;
				}
				$.each(listTable.trTemplete, function(i,tdTemplete) {
					var tdDom=trDom.tdList[i];
					if(tdTemplete.tbodyTd!==undefined){
						if(tdTemplete.tbodyTd.update!==undefined){
							tdTemplete.tbodyTd.update(tdDom.$dom,trDom.trRoot,trDom.data);
						}
					}
				});
				listTable.resize();
			}
		}
		function addTr(trData,isPrepend){
			if(trData.id===undefined){
				trData.id=listTable.trIdIndex;
				listTable.trIdIndex++;
			}
			var tbodyTrDom=new TbodyTrDom();
			if(isPrepend){
				tbodyTrDom.$dom.prependTo(listTable.$tbody);
			}else{
				tbodyTrDom.$dom.appendTo(listTable.$tbody);
			}
			tbodyTrDom.update(trData);
			listTable.tbodyTrList[trData.id]=tbodyTrDom
		}

		var windowResizeTimeout;
		listTable.resize=function(){
			clearTimeout(windowResizeTimeout);
			windowResizeTimeout=setTimeout(function(){
				var $theadTh=listTable.$theadTr.children('.td');
				var $tbodyTr=listTable.$tbody.children('.tr');
				listTable.$tbodyScrollBox.css('top',listTable.$thead.outerHeight());
				if($tbodyTr.length<1){
					listTable.$thead.css('width','100%');
				}else{
					listTable.$thead.css('width','');
					$tbodyTr.eq(0).children('.td').each(function(i,tdDom){
						$theadTh.eq(i).css({
							width:$(tdDom).outerWidth()
						});
					})
				}
				listTable.$tbodyScrollBox.css('top',listTable.$thead.outerHeight());
			},20);
		}
		listTable.appendTr=function(){
			var trDataList=Array.prototype.slice.call(arguments);
			$.each(trDataList, function(i,trData) {
				addTr(trData);
			});
			listTable.resize();
		}
		listTable.clear=function(){
			listTable.$tbody.html('');
			listTable.tbodyTrList={};
			listTable.trIdIndex=0;
		}








		$(window).on('resize',function(){
			listTable.resize();
		});


	}
	window.ListTable=ListTable;
	return ListTable;
})
